package db
import (
	"github.com/go-xorm/xorm"
	_ "github.com/lib/pq"

)


type Commodity struct {
	Id         int    `xorm:"not null pk autoincr INTEGER" schema:"id"`
	Name 	   string `xorm:"VARCHAR(20)" schema:"name"`
	Stock      int       `xorm:"INTEGER" schema:"stock"`
	ComClass   int  	`xorm:"INTEGER" schema:"comclass"`
	Sold       int  	`xorm:"INTEGER" schema:"sold"`
	Price      float64    `xorm:"Double" schema:"price"`
}
type Bill struct {
	Id   	   int    `xorm:"not null pk autoincr INTEGER"`
	ComId      int       `xorm:"INTEGER"`
	Amount    int       `xorm:"INTEGER"`
	BillAllId     int       `xorm:"INTEGER"`
}
type BillAll struct {
	Id   	   int    `xorm:"not null pk autoincr INTEGER"`
	UserId     int       `xorm:"INTEGER"`
	Numbers    int       `xorm:"INTEGER"`
	Psf        float64    `xorm:"Double"`
	State      int       `xorm:"INTEGER"`
}
type Users struct {
	Id         int    `xorm:"not null pk autoincr INTEGER"`
	Accounts   string `xorm:"VARCHAR(20)"`
	Name   	   string `xorm:"VARCHAR(20)"`
	Password   string `xorm:"VARCHAR(20)"`
	Phone      string `xorm:"VARCHAR(12)"`
	Building   string `xorm:"VARCHAR(10)"`
	Dorm   	   string `xorm:"VARCHAR(6)"`
	Powers     int       `xorm:"not null default 0 INTEGER"`
	Cash       float64    `xorm:"default 20.00 Double"`
	HaveImg    int `xorm:"default 0 INTEGER"`
}

var engine *xorm.Engine
func init() {
	engine, _ = xorm.NewEngine("postgres", "user=postgres password=123456 dbname=myshop host=127.0.0.1 port=5432 sslmode=disable")
	InitModels()
}
func InitModels() {
	engine := GetEngine()
	err := engine.Sync2(
		new(Commodity),
		new(Bill),
		new(BillAll),
		new(Users),

	)
	if err != nil {
		println(err.Error())
		panic("tables error")
	}
}
func Query(sql string) (rowArray []map[string][]byte){
	rowArray, _ = engine.Query(sql)
	return  rowArray
}
func GetEngine() *xorm.Engine {
	if engine == nil {
		panic("db init fail")
	}

	return engine
}
